<?php namespace App\Infra;

use App\Utils\ModelOperatorTrait;
use App\Models\Customer;
use App\Models\CustomerInfo;

class CustomerManager{

	use ModelOperatorTrait;

	public function getCustomers($conditions = null,$first = false){
		return $this->findModel(Customer::class,Customer::query()->orderBy('created_at','desc'),$conditions,$first);
	}

	public function updateCustomer($object = null,$data= null){
		return $object->update($data);
	}

	public function createCustomer($data){
		return $this->create(Customer::class,$data);
	}

	public function deleteCustomer($object = null,$id = null){
		if($object != null){
			$object->delete();

			return;
		}
		if($id != null){
			Customer::find($id)->delete();

			return;
		}
	}

	public function createInfo($data){
		return CustomerInfo::create($data);
	}

	public function getCustomerInfos($conditions,$first = false){
		return $this->findModel(CustomerInfo::class,CustomerInfo::query(),$conditions,$first)->orderBy('created_at','desc');
	}

	public function getInfoByCustomer($conditions = null,$first = false){
		return $this->findModel(Customer::class,Customer::query()->with(['customerInfo']),$conditions,$first)->orderBy('created_at','desc');
	}

	public function updateInfo($object,$data){
		$object->update($data);

		return $object;
	}
}